#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

typedef struct Node {
    char data;
    struct Node *next;
} Node;

Node * reverse(Node *list, Node *reversed_list)
{
    if (NULL == list)
    {
        return reversed_list;
    }
    else
    {
        Node *next = list->next;
        list->next = reversed_list;
        return reverse(next, list);
    }
}

Node * reverse_list(Node *list)
{
    return reverse(list, NULL);
}

void print_list(Node * node)
{
    if (node != NULL)
    {
        printf("%c", node->data);
        if (node->next != NULL)
        {
            printf("->");
            print_list(node->next);
        }
        else
        {
            printf("\n");
        }
    }
}

Node *add_node(Node **list, Node *node)
{
    if (*list == NULL)
    {
        *list = node;
    }
    else
    {
        add_node(&(*list)->next, node);
    }
    return *list;
}

Node * new_node(char c)
{
    Node *node = calloc(1, sizeof(Node));
    node->data = c;
    return node;
}

Node * list_init_from_stdin(void)
{
    Node *list = NULL;
    int c = getchar();
    while (c != EOF)
    {
        if (isgraph(c))
        {
            add_node(&list, new_node(c));
        }
        c = getchar();
    }

    rewind(stdin);

    return list;
}

void release_list(Node *node)
{
    if (node == NULL)
    {
        return;
    }
    if (node->next != NULL)
    {
        release_list(node->next);
    }

    free(node);
}

int main(int argc, const char *argv[])
{
    printf("input some characters to init a list. ( EOF (ctr + z) on windows or (ctr + d) on *nix) to finish input)\n");

    Node *list = list_init_from_stdin();

    printf("\n");
    print_list(list);

    list = reverse_list(list);

    print_list(list);

    release_list(list);
    
    return 0;
}
